<template>
  <a-card title="自定义信息" :bordered="true" style="margin-top: 7px">
    <a-form :form="form" layout="vertical" ref='formRef'>
      <template v-for="(p, index) in formTypeExtendedFields">
        <a-form-item :required="p.fieldMust == 2">
          <span slot="label">
            {{ p.fieldName }}
            <a-tooltip v-if="p.explanationPrompt" :title="p.explanationPrompt">
              <a-icon type="info-circle" style="color: rgba(0, 0, 0, 0.45)" />
            </a-tooltip>
          </span>

          <a-input :disabled="p.fieldMust == 1" :placeholder="p.placeholder" v-if="p.fieldType == 1" v-decorator="[
            'extDataJson.' + p.fieldKey,
            { rules: [{ required: p.fieldMust == 2, message: '请输入内容' }] },
          ]">
          </a-input>

          <template v-if="p.fieldType == 2">
            <a-textarea :disabled="p.fieldMust == 1" :placeholder="p.placeholder" v-decorator="[
              'extDataJson.' + p.fieldKey,
              { rules: [{ required: p.fieldMust == 2, message: '请输入内容' }] },
            ]" :auto-size="{ minRows: 1, maxRows: 2 }">
            </a-textarea>
          </template>

          <a-input-number style="width: 100%" :disabled="p.fieldMust == 1" :placeholder="p.placeholder"
            :formatter="value => value.substr(0, p.fieldLength)" :parser="value => value.substr(0, p.fieldLength)"
            :min="0" :precision="0" v-if="p.fieldType == 3" v-decorator="[
              'extDataJson.' + p.fieldKey,
              { rules: [{ required: p.fieldMust == 2, message: '请输入内容' }] },
            ]">
          </a-input-number>

          <a-input-number v-if="p.fieldType == 4" :step="0.0001" style="width: 100%" :disabled="p.fieldMust == 1"
            :placeholder="p.placeholder" :min="0.0001" :max="getMaxValueForDigits(p.fieldLength * 1)" v-decorator="[
              'extDataJson.' + p.fieldKey,
              { rules: [{ required: p.fieldMust == 2, message: '请输入内容' }] },
            ]" :formatter="(value) => onInput(value)" />
        </a-form-item>
      </template>
      <a-form-item v-for="(p, index) in formTypeExtendedFields.length % 3" :key="index"></a-form-item>
    </a-form>
  </a-card>
</template>

<script>
import { APILX } from '@/api/requestLx'

export default {
  props: {
    formType: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this, { name: 'coordinated2' }),
      formTypeExtendedFields: [],
    }
  },
  created() {
    this.formTypeExtendedFieldList()
  },
  methods: {
    onInput(event) {
      let value = event + ''
      // 截断小数点后超过4位的部分
      if (value.includes('.')) {
        let parts = value.split('.')
        if (parts[1].length > 4) {
          value = parts[0] + '.' + parts[1].substring(0, 4)
        }
      } else {
        value = event
      }
      return value
    },
    getMaxValueForDigits(digitCount) {
      if (digitCount <= 0) {
        throw new Error('Digit count must be a positive integer.')
      }
      return (Math.pow(10, digitCount) - 1) + 0.9999
    },
    async formTypeExtendedFieldList() {
      let res = await APILX.formTypeExtendedFieldList({ formType: this.formType })
      if (res.code == 200 && res.result) {
        this.formTypeExtendedFields = res.result
        console.log(this.formTypeExtendedFields, 'this.formTypeExtendedFields')
      //   如果是只读，就把placeholder的值赋给该项
        this.formTypeExtendedFields.forEach(item=>{
          if(item.fieldMust==1){
            console.log(item,'只读项',this.$refs.formRef)
            setTimeout(() => {
              this.$refs.formRef.form.setFields({
                [item.fieldKey]:'888',
                we:'98'
              })
            }, 1000)
          }
        })
      }
    },
    setFields(x) {
      console.log(x, 'xxxxxxx')
      this.form.setFieldsValue(x)
    },
  },
}
</script>

<style lang="less" scoped>
// /deep/ .ant-card-body {
//   //padding: 24px 0;
// }</style>
